package com.example.javaee_library.dao;

import com.example.javaee_library.Entity.Appointment;
import com.example.javaee_library.Entity.action;
import org.apache.ibatis.annotations.*;

import java.sql.Timestamp;
import java.util.List;

@Mapper
public interface AppointmentDao {

	/**
	 * 插入预约图书记录
	 * 
	 * @param bookId
	 * @param studentId
	 * ignore则会忽略导致错误的行，并将其余行插入到表中。
	 * @return 插入的行数
	 */
	@Insert("INSERT ignore INTO appointment (book_id, student_id)" +
			"VALUES (#{bookId}, #{studentId})")
	int insertAppointment(@Param("bookId") long bookId, @Param("studentId") long studentId);

	/**
	 * 通过主键查询预约图书记录，并且携带图书实体
	 * 
	 * @param bookId
	 * @param studentId
	 * @return
	 */
	@Select("SELECT\n" +
			"a.book_id,\n" +
			"a.student_id,\n" +
			"a.appoint_time,\n" +
			"b.book_id \"book.book_id\",\n" +
			"b.`name` \"book.name\",\n" +
			"b.number \"book.number\"\n" +
			"FROM\n" +
			"appointment a\n" +
			"INNER JOIN book b ON a.book_id = b.book_id\n" +
			"WHERE\n" +
			"a.book_id = #{bookId}\n" +
			"AND a.student_id = #{studentId}")
	Appointment queryByKeyWithBook(@Param("bookId") long bookId, @Param("studentId") long studentId);
//mysql的级联查询

	@Select("select a.book_id,a.student_id,a.appoint_time,b.book_id \"book.book_id\",b.`name` \"book.name\",b.number \"book.number\"\n" +
			"from appointment a\n" +
			"inner join book b on a.book_id = b.book_id\n" +
			"where  a.student_id = #{studentId};")
	List<Appointment> queryBystudentWithBook(@Param("studentId") long studentId);


	@Insert("INSERT ignore INTO action (book_id, student_id,operation)\n" +
			"values (#{bookId},#{studentId},#{operation})")
	int Insertaction(long bookId,  long studentId, String operation);

	@Select("select * from action")
	List<action> Allaction();

	@Select("select * from action where student_id like #{student_id}")
	List<action> Action_Bystudent_id(String student_id);

	@Select("select * from action where book_id like #{book_id}")
	List<action> Action_Bybook_id(String book_id);

	@Delete("delete from action where id = #{id}")
	int deleteaction(int id);

	@Update("update appointment set appoint_time = #{timestamp} where book_id = #{bookId} and student_id = #{studentId}")
	int Appoint_again(long bookId, long studentId, Timestamp timestamp);


}
